package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.dao.CourierStationDao;
import com.hs.panghu.dao.impl.CommunityDaoImpl;
import com.hs.panghu.dao.impl.CourierStationImpl;
import com.hs.panghu.entity.Community;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courierStation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        // 连接数据库
        CourierStationDao courierStationDao = new CourierStationImpl();
        if ("courierStationList".equals(action)) {  // 分页查询
            Map<String, String> map = new HashMap<>();
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String courierStationName = req.getParameter("courierStationName");
            String communityName = req.getParameter("communityName");
            String courierStationStatus = req.getParameter("courierStationStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("courierStationName", courierStationName);
            map.put("communityName", communityName);
            map.put("courierStationStatus", courierStationStatus);
            map.put("startTime", startTime);
            map.put("endTime", endTime);

            // 动态获取社区集合信息
            List<CourierStation> list = courierStationDao.selectCourierStations(map);
            // 动态获取社区的数据总条数
            int total = courierStationDao.selectCourierStationCount(map);
            // 创建一个返回值的map集合，用来包裹list数据和total总数量
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            //把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("courierStationStatus".equals(action)) {
            String courierStationId = req.getParameter("courierStationId");
            String courierStationStatus = req.getParameter("courierStationStatus");
            if (courierStationStatus==null){
                courierStationStatus="0";
            }
            //组成对象
            CourierStation courierStation = new CourierStation(
                    Integer.parseInt(courierStationId),
                    Integer.parseInt(courierStationStatus)
            );
            int row = courierStationDao.updateStatus(courierStation);

            if (row != 0) {
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            } else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        } else if ("addPost".equals(action)) {
            String courierStationAddress = req.getParameter("courierStationAddress");
            System.out.println(courierStationAddress);
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationCommunityId = req.getParameter("courierStationCommunityId");
            if(courierStationCommunityId.equals("")){
                courierStationCommunityId="0";
            }
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            CourierStation courierStation = new CourierStation(
                    courierStationAddress,
                    courierStationCity,
                    Integer.parseInt(courierStationCommunityId),
                    courierStationDistrict,
                    courierStationImage,
                    courierStationName,
                    courierStationProvince
            );

            int row = courierStationDao.insertCourierStation(courierStation);
            if(row!=0) {
                resp.getWriter().write(JSON.toJSONString(Result.success("添加成功")));
            }else {
                resp.getWriter().write(JSON.toJSONString(Result.error("添加失败")));
            }
        }//修改servlet
        else if ("updatePost".equals(action)) {
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String courierStationCommunityId = req.getParameter("courierStationCommunityId");
            if(courierStationCommunityId.equals("")){
                courierStationCommunityId="0";
            }
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationId = req.getParameter("courierStationId");
            if(courierStationId.equals("")){
                courierStationId="0";
            }
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationOrderCount = req.getParameter("courierStationOrderCount");
            if(courierStationOrderCount.equals("")){
                courierStationOrderCount="0";
            }
            String courierStationSort = req.getParameter("courierStationSort");
            if(courierStationSort.equals("")){
                courierStationSort="0";
            }
            String courierStationStatus = req.getParameter("courierStationStatus");
            if(courierStationStatus.equals("")) {
                courierStationStatus = "0";
            }

            System.out.println(courierStationStatus);
            String courierStationProvince = req.getParameter("courierStationProvince");
            CourierStation courierStation = new CourierStation(
                    courierStationAddress,
                    courierStationCity,
                    Integer.parseInt(courierStationCommunityId),
                    courierStationCommunityName,
                    courierStationImage,
                    courierStationDistrict,
                    courierStationName,
                    Integer.parseInt(courierStationOrderCount),

                    courierStationProvince,
                    Integer.parseInt(courierStationSort),
                    Integer.parseInt(courierStationStatus),
                    Integer.parseInt(courierStationId)
            );

            int row = courierStationDao.updateCourierStation(courierStation);
            if(row!=0) {
                resp.getWriter().write(JSON.toJSONString(Result.success()));
            }else {
                resp.getWriter().write(JSON.toJSONString(Result.error()));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
